<template>
    <div>
      <div style="padding: 10px 0;">
      <el-input style="width: 200px;" placeholder="请输入名称搜索" v-model="roleName" suffix-icon="el-icon-search" >
      </el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load()"> 搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="reset()"> 重置</el-button>
      </div>
      <div style="padding: 10px 0;">
      <el-button type="primary" style="margin-left: 10px;" @click="add()"> 新增</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="delAll()"> 批量删除</el-button>
      </div>

<el-dialog title="新增角色" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="角色名称" >
            <el-input v-model="form.roleName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="角色描述" >
            <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
         </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addRole()" >确 定</el-button>
  </div>
</el-dialog>

<el-dialog title="修改角色" :visible.sync="editFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="角色名称" >
            <el-input v-model="form.roleName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="角色描述" >
            <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
         </el-form-item>

    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editRole()" >确 定</el-button>
  </div>
</el-dialog>

<el-dialog title="分配菜单" :visible.sync="dialogSelectVisible"
ref="tree" append-to-bodywidth="30%">
  <el-tree
  :props="props"
  :data="menuData"
  :check-strictly="true"
  node-key="menuId"
  ref="tree"
  show-checkbox
  :default-expanded-keys="expends"
  @check-change="handleCheckChange()">
</el-tree>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogSelectVisible = false">取 消</el-button>
    <el-button type="primary" @click="addRoleMenus()" >确 定</el-button>
  </div>
</el-dialog>

      <el-table :data="tableData" :border="true" >
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="roleId" label="编号" v-if="false">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="selectMenu(scope.row.roleId)">分配菜单</el-button>
            <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>

            <el-popconfirm
            style="margin-left: 5px;"
  confirm-button-text='好的'
  cancel-button-text='不用了'
  icon="el-icon-info"
  icon-color="red"
  @confirm="del(scope.row)"
  title="您确定删除吗？"
>
<el-button type="text" slot="reference" size="small">删除</el-button>
</el-popconfirm>

          </template>
        </el-table-column>
      </el-table>

  <div class="block" style="padding: 20px 0;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=total>
    </el-pagination>
  </div>

    </div>
</template>
<script>
import { getRoleListByPost, save, update, delRole, addRoleMenus, getRoleMenus } from '../api/Role.js'
import { findAll } from '../api/Menu.js'

export default {
  data () {
    return {
      tableData: [],
      multipleSelection: [],
      flag: false,
      asidWith: '',
      iconFold: 'el-icon-s-fold',
      total: 0,
      pageNum: 1,
      pageSize: 2,
      roleName: '',
      tblroleId: 0,
      dialogFormVisible: false,
      editFormVisible: false,
      dialogSelectVisible: false,
      menuData: [],
      props: {
        label: 'menuName'
      },
      expends: [],
      form: {
        roleId: '',
        roleName: '',
        roleDesc: ''
      }
    }
  },
  created () {
    this.load();
  },
  computed: {
  },
  methods: {
    load () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orderName: this.orderName
      }
      getRoleListByPost(params).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        console.log(err)
      })
    },

    reset () {
      this.orderName = '';
      this.load();
    },
    resetForm () {
      this.form = {}
    },
    handleSizeChange (pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load();
    },
    handleCurrentChange (pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum
      this.load();
    },
    add () {
      this.form = {}
      this.dialogFormVisible = true
    },
    del (row) {
      delRole(row).then(res => {
        this.$message.success('删除成功')
        this.load()
      })
    },
    edit (row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.editFormVisible = true
    },
    editRole () {
      update(this.form).then(res => {
        this.$message.success('修改成功')
      })
      this.editFormVisible = false
      this.load()
    },
    search () {
      console.log('搜索')
    },
    addRole () {
      save(this.form).then(res => {
        this.$message.success('添加成功')
      })
      this.dialogFormVisible = false
      this.resetForm()
      this.load()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAll () {
      this.multipleSelection.forEach(item => {
        delRole(item).then(res => {
          this.$message.success('删除成功')
          this.load()
        })
      })
    },
    selectMenu (roleId) {
      this.dialogSelectVisible = true;
      this.tblroleId = roleId;

      findAll().then(res => {
        this.menuData = res
        this.expends = this.menuData.map(item => item.menuId)
        console.log(res)
      }).catch(err => {
        console.log(err)
      });

      getRoleMenus(roleId).then(res => {
        this.$refs.tree.setCheckedKeys(res.data);
      });
    },
    handleCheckChange (data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    addRoleMenus () {
      addRoleMenus(this.tblroleId, this.$refs.tree.getCheckedKeys()).then(res => {
        this.$message.success('分配成功')
        this.dialogSelectVisible = false;
        console.log(res)
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
